﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;
using Entities.IRepository;
using DAL;
using BLL;

namespace HeThongNhaSach
{
    public partial class frmThemNhanVien : Form
    {
      
        public NhanVien nv;
        GeneralRepository<NhanVien> _NVRepo;
        QLNhanVien _NVManager;
        public frmMain frmChinh = new frmMain();
        public frmThemNhanVien()
        {
            InitializeComponent();
            this.FormClosed += new FormClosedEventHandler(btnHuy_Click);
            GeneralRepository<TaiKhoan> _TKRepo = new GeneralRepository<TaiKhoan>();
            
            List<TaiKhoan> lnv = _TKRepo.GetAll().ToList();
            foreach (TaiKhoan tk in lnv)
            cbbTaiKhoan.Items.Add(tk.Username);
           
        
        }
     /// <summary>
     /// tạo mã nhân viên
     /// </summary>
     /// <param name="ngay"></param>
     /// <returns></returns>
        private string TaoMaNV(DateTime ngay)
        {
            List<NhanVien> l = _NVRepo.GetAll().ToList().FindAll(delegate(NhanVien hd)
            {
                return hd.NgaySinh.Date == DateTime.Now.Date;
            });
            int stt = 0;
            if (l.Count > 0)
            {
                stt = int.Parse(l[0].MaNhanVien.Substring(6, 2));
                foreach (NhanVien hd in l)
                {
                    int ma = int.Parse(hd.MaNhanVien.Substring(6, 4));
                    if (ma > stt)
                        stt = ma;
                }
            }
            return "NV" +  (stt + 1).ToString().PadLeft(8, '0');
        }
        /// <summary>
        /// thêm nhân viên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnThemNV_Click(object sender, EventArgs e)
        {

            NhanVien nv = new NhanVien();
            nv.MaNhanVien = TaoMaNV(DateTime.Now.Date);
            nv.HoTen = txtHoTen.Text;
            nv.NgaySinh = dtpNgaySinh.Value;
            nv.SoDienThoai = int.Parse(txtSoDienThoai.Text);
            nv.DiaChi = txtDiaChi.Text;
            nv.ChucVu = cboChucVu.Text;
            nv.Username = cbbTaiKhoan.Text;
            nv.TrangThai = cboTrangThai.Text;
            if (rdNam.Checked)
            {
                nv.GioiTinh = "Nam";

            }
            else
                nv.GioiTinh = "Nư";
            try
            {
                nv.LuongCoBan = float.Parse(txtLuong.Text);
            }
            catch
            {
                MessageBox.Show("Nhập lại lương cơ bản");
            }
            var vResult = _NVManager.Validate(nv);
            if (vResult.Valid)
            {
                var aResult = _NVManager.Insert(nv);
                if (aResult.Success)
                {

                }
                else
                    MessageBox.Show(aResult.GetErrorMessages());
            }
            else MessageBox.Show(vResult.ToString());
            frmChinh.RefreshNV();
            
          
              

              
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// mặc định của form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMacDinhNV_Click(object sender, EventArgs e)
        {
            txtMaNV.Text = null;
            txtHoTen.Text = null;
            dtpNgaySinh.Value = DateTime.Now;
            txtSoDienThoai.Text = null;
            txtDiaChi.Text = null;
            txtLuong.Text = null;
            cboChucVu.SelectedIndex = 0;
            cbbTaiKhoan.Text = null;
            cboTrangThai.SelectedIndex = 0;
            rdNam.Checked = true;
        }

        private void frmThemNhanVien_Load(object sender, EventArgs e)
        {
            _NVRepo = new GeneralRepository<NhanVien>();
            _NVManager = new QLNhanVien(_NVRepo);
            try
            {
                txtMaNV.Text = nv.MaNhanVien;
                txtHoTen.Text = nv.HoTen;
                dtpNgaySinh.Value = nv.NgaySinh;
                txtSoDienThoai.Text = nv.SoDienThoai.ToString();
                txtDiaChi.Text = nv.DiaChi;
                cboChucVu.Text = nv.ChucVu;
                cbbTaiKhoan.Text = nv.Username;
                cboTrangThai.Text = nv.TrangThai;
                txtLuong.Text = nv.LuongCoBan.ToString();
                
                
            }
            catch { }
        }
        /// <summary>
        /// sửa thông tin nhân viên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCapNhatNhanVien_Click(object sender, EventArgs e)
        {
            NhanVien nv = new NhanVien();
            nv.MaNhanVien = txtMaNV.Text;
            nv.HoTen = txtHoTen.Text;
            nv.NgaySinh = dtpNgaySinh.Value;
            nv.SoDienThoai = int.Parse(txtSoDienThoai.Text);
            nv.DiaChi = txtDiaChi.Text;
            nv.ChucVu = cboChucVu.Text;
            nv.Username = cbbTaiKhoan.Text;
            nv.TrangThai = cboTrangThai.Text;
            if (rdNam.Checked)
            {
                nv.GioiTinh = "Nam";

            }
            else
                nv.GioiTinh = "Nư";
            try
            {
                nv.LuongCoBan = float.Parse(txtLuong.Text);
            }
            catch
            {
                MessageBox.Show("Nhập lại lương cơ bản");
            }
            var vResult = _NVManager.Validate(nv);
            if (vResult.Valid)
            {
                var aResult = _NVManager.Update(nv);
                if (aResult.Success)
                {
                }
                else
                    MessageBox.Show(aResult.GetErrorMessages());
            }
            else MessageBox.Show(vResult.ToString());
            frmChinh.RefreshNV();

        }
    }
}
